//
//  OtherCitiesViewController.m
//  Lovelife
//
//  Created by dllo on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "OtherCitiesViewController.h"
#import "OtherCitIesCell.h"
#import "CityModel.h"
#import "AFNetWorkingYU.h"
#import "UIImageView+WebCache.h"
#import "CityViewController.h"
#import "UIScrollView+MJRefresh.h"
#import "NightManager.h"
@interface OtherCitiesViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicatorView;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *arrayOfCity;
@property (nonatomic, assign) int page;
@end

@implementation OtherCitiesViewController
- (void)dealloc
{
    [_activityIndicatorView release];
    [_flowLayout release];
    [_collectionView release];
    [_arrayOfCity release];
    [_countryId release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfCity = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"全部城市";
    
    // 旋转进度轮
    self.activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, 150, 100, 100)] ;
    [self.activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhiteLarge];
    self.activityIndicatorView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:self.activityIndicatorView];
    [_activityIndicatorView release];
    // 获取数据
    [self getData];

}
- (void)getData
{
    if (self.page == 1) {
        [self.activityIndicatorView startAnimating];
    }
    int count = self.arrayOfCity.count;
    [AFNetWorkingYU netWorkingHanderGetGataWithUrl:[NSString stringWithFormat:@"http://open.qyer.com/place/city/get_city_list?app_installtime=1429487962&client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&countryid=%@&lat=38.88341543254953&lon=121.5392017636654&page=%d",self.countryId,self.page] resultBlock:^(id result) {
        NSMutableArray *array = result[@"data"];
        for (NSDictionary *dic in array) {
            CityModel *city = [[CityModel alloc]init];
            [city setValuesForKeysWithDictionary:dic];
            [self.arrayOfCity addObject:city];
            [city release];
        }
        if (self.page == 1) {
            [self createView];
            [self.activityIndicatorView stopAnimating];
        }
        if (self.arrayOfCity.count == count) {
            self.collectionView.footerPullToRefreshText = @"没有更多内容了!";
            self.collectionView.footerReleaseToRefreshText = @"没有更多内容了!";
        }
        
        [self.collectionView footerEndRefreshing];
        [self.collectionView reloadData];

    }];
}
- (void)createView
{
    // 创建item
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(130, 180);
    self.flowLayout.scrollDirection = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 10, 20);
    self.flowLayout.minimumInteritemSpacing = 20;
    self.flowLayout.minimumLineSpacing = 20;
//    self.flowLayout.footerReferenceSize = CGSizeMake(20, 120);
    [_flowLayout release];
    
    // 创建瀑布流
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:self.flowLayout];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    // 注册
    [self.collectionView registerClass:[OtherCitIesCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
//    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
    [self addFooter];
}
- (void)addFooter
{
    // 添加上拉刷新尾部控件
    [self.collectionView addFooterWithCallback:^{
        [self add];
        
            }];
}
- (void)add{
    self.page++;
    [self getData];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrayOfCity.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    OtherCitIesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    CityModel *city = self.arrayOfCity[indexPath.row];
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.nameLabel.text = [NSString stringWithFormat:@" %@\n %@",city.catename,city.catename_en];
    cell.sceneryLabel.text = [NSString stringWithFormat:@"代表景点:%@",city.representative];
;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CityViewController *cityVC = [[CityViewController alloc]init];
    CityModel *city = self.arrayOfCity[indexPath.row];
    cityVC.cityId = city.cityId;
    [self.navigationController pushViewController:cityVC animated:YES];
    [cityVC release];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"head" forIndexPath:indexPath];
        return head;
//    }else{
//        UICollectionReusableView *foot = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"foot" forIndexPath:indexPath];
//        return foot;
//    }
}
- (void)setLightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
}
- (void)setNightDayModel
{
    [NightManager setBackgroundColorWithView:self.view];
    [NightManager setBackgroundColorWithCollectionView:self.collectionView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
